﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FrameWork.Lib
{
    public static class IOExt
    {
        public static string ReadAllText(this System.IO.StreamReader pStreamReader)
        {
            var sb = new StringBuilder();

            while (!pStreamReader.EndOfStream)
            {
                var str = pStreamReader.ReadLine().Trim();
                if (str.Length != 0)
                {
                    sb.AppendLine(str);
                }
            }

            return sb.ToString();
        }

        public static void SaveAllText(this string pValue, string pFileName)
        {
            using (var sw = new System.IO.StreamWriter(pFileName))
            {
                sw.Write(pValue);
                sw.Close();
            }
        }

        public static byte[] NextRead(this System.IO.BinaryReader pBinaryReader, out bool pValid)
        {
            byte[] buffer = new byte[1024];
            pValid = pBinaryReader.Read(buffer, 0, 1024) > 0;
            return buffer;
        }

        public static void MakeWritable(this System.IO.FileInfo pInfo)
        {
            if ((pInfo.Attributes & System.IO.FileAttributes.ReadOnly) == System.IO.FileAttributes.ReadOnly)
            {
                pInfo.Attributes = (System.IO.FileAttributes)(((int)pInfo.Attributes) & (~(int)System.IO.FileAttributes.ReadOnly));
            }
        }
    }
}
